import {Component, EventEmitter, Input, OnInit, Output} from '@angular/core';
import {FormBuilder, FormGroup, Validators} from '@angular/forms';
import {BaseService} from '../../../shared/service/base.service';
import {validMoney} from '../../../shared/validator/validator';
import {ApiUrl} from '../../../config/api-url';

@Component({
  selector: 'app-device-add',
  templateUrl: './device-add.component.html',
  styleUrls: ['./device-add.component.less']
})
export class DeviceAddComponent implements OnInit {
// 模态框显隐
  @Input()
  isVisible = false;
  // 父子互传值
  @Output()
  emit: EventEmitter<any> = new EventEmitter();
  // 提交时提交按钮是否加载中
  isOkLoading: any;
  // 模态框表单
  formModel: FormGroup;
  title: any;
  constructor(private fb: FormBuilder, private baseService: BaseService) { }

  ngOnInit(): void {
    this.buildForm();
  }
  buildForm() {
    this.formModel = this.fb.group({
      startNo: [null, [Validators.required]],
      endNo: [null, [Validators.required]]
    });
  }

  // 取消
  handleCancel() {
    this.isVisible = false;
    this.formModel.reset({
      startNo: null,
      endNo: null
    });
    this.emit.emit(this.isVisible);
  }

  // 确认
  handleOk() {
    this.isOkLoading = true;
    if (!this.baseService.zorro.validateFormValidate(this.formModel)) {
      this.isOkLoading = false;
      return;
    }

    if (!this.isOkLoading) {
      return;
    }
    this.baseService.post(ApiUrl.DeviceUrl + '/batchRegisterDevice', this.formModel.value).subscribe(res => {
      if (res.status == 0) {
        this.baseService.layer.messageSuccess(res.data);
        this.handleCancel();
        this.isOkLoading = false;
      } else {
        this.baseService.layer.messageError(res.msg);
        this.isOkLoading = false;
      }
    });
  }
}
